/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.unmsm.fisi.ceiecbt.app.servlets;

import java.io.*;
import java.net.*;


import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

import pe.edu.unmsm.fisi.ceiecbt.ejb.beans.ComtbEmpresaFacadeRemote;
import pe.edu.unmsm.fisi.ceiecbt.ejb.beans.PortafolioInversionRemote;
import pe.edu.unmsm.fisi.ceiecbt.ejb.beans.ComtbIndicadoresFacadeRemote;
import pe.edu.unmsm.fisi.ceiecbt.ejb.entidad.ComtbDetalleInversion;
import pe.edu.unmsm.fisi.ceiecbt.ejb.entidad.ComtbInversion;
import pe.edu.unmsm.fisi.ceiecbt.ejb.entidad.ComtbIndicadores;
import pe.edu.unmsm.fisi.ceiecbt.ejb.entidad.ComtbEmpresa;

/**
 *
 * @author Liz Alvarez
 */
public class Inversion extends HttpServlet {
   
     @EJB
      private PortafolioInversionRemote gpr;
     @EJB
      private ComtbEmpresaFacadeRemote comtber;
     @EJB
      private ComtbIndicadoresFacadeRemote comtbind;
     
     ComtbDetalleInversion cdinv;
     
     ComtbInversion cinv;
     
     ComtbIndicadores ctbind;
    
    // ComtbEmpresa comtbemp;
    
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        cdinv= new ComtbDetalleInversion();
        cinv = new ComtbInversion();
        try {
           if(request.getParameter("anadir")!=null){
            
            int tippat=Integer.parseInt(request.getParameter("IdTipoPatrocinador"));
            
            int totAcc=0;
            
            String idemp=request.getParameter("IdEmpresa");
            
            int numacc=0;
            
            int idperfil=0;
            
            float monto=0;
            
            ComtbEmpresa comtbemp=comtber.find(idemp);
            
            if(tippat==2){
                
            idperfil=Integer.parseInt(request.getParameter("IdPerfil"));
            monto=Float.parseFloat(request.getParameter("ValMonto"));
            
            }
            else{
         
                
             //   if(lind.size()>0)
            //   totAcc= Integer.parseInt(lind.get(0).getIndicvValor().toString().substring(0, lind.get(0).getIndicvValor().toString().indexOf(".")));
                    
                numacc=Integer.parseInt(request.getParameter("numAcc"));
                monto=Float.parseFloat(request.getParameter("valAcc"))*numacc;
            }
               
            cdinv.setDetinviTipoInv(tippat);
            cdinv.setDetinviPerfil(idperfil);
            cdinv.setDetinvrMonto(monto);
            cdinv.setEmprevCodEmpresa(comtbemp);
            cdinv.setDetinviNumAcciones(numacc);
            cdinv.setInvervCodSolicitud(cinv);
            
            HttpSession sesion_act=request.getSession(true);
            gpr=(PortafolioInversionRemote) sesion_act.getAttribute("PortafolioInversionBean");
            
            gpr.addDetInversion(cdinv);
            
                //HttpSession sesion_ac=request.getSession(true);
              //  PortafolioInversionRemote portinv=(PortafolioInversionRemote) sesion_act.getAttribute("PortafolioInversionBean");
            ArrayList ind5=new ArrayList();
            
            ArrayList<ComtbDetalleInversion> arlinv=gpr.getInversion();
                
                for(int i=0;i<arlinv.size();i++){
  
                    if(arlinv.get(i).getDetinviTipoInv()==1){
                        
                        List<ComtbIndicadores> lind=comtbind.findByEmprevTipInd(Integer.parseInt(arlinv.get(i).getEmprevCodEmpresa().getEmprevCodEmpresa().toString()), 5);
                        ind5.add(Integer.parseInt(lind.get(0).getIndicvValor().toString().substring(0, lind.get(0).getIndicvValor().toString().indexOf("."))));
                    }
                }
                        
            request.setAttribute("totAcci", ind5);
            request.getRequestDispatcher("verCanasta.jsp").forward(request, response);
               
           }
            
            
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
